package com.colorful.insuranceproject.claims;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.adapter.CityListBean;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.PhysicalExaminationAppointmentBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.consts.URLConst;
import com.colorful.insuranceproject.request.ClaimsRequest;
import com.colorful.insuranceproject.utils.ParseHtmlCode;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/10/30.
 * <p>
 * 体检详情页
 */
public class PhysicalExaminationDetailsActivity extends TitleActivity {

    private static final int GET_PHYSICAL_DETAILS_URL = 0X1;

    ClaimsRequest claimsRequest;

    public static final String EXTRA_DETAILS = "extra_data";

    public static final String EXTRA_POSITION = "extra_position";

    PhysicalExaminationAppointmentBean pb;

    @Bind(R.id.webView)
    WebView webView;

    @Bind(R.id.tv_title)
    TextView tvTitle;

    @Bind(R.id.tv_hint)
    TextView tvHint;

    @Bind(R.id.tv_money)
    TextView money;

    CityListBean cb;

    @Override
    protected int getContentView() {
        return R.layout.activity_physical_examination;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.physical_details));
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        pb = bundle.getParcelable(EXTRA_DETAILS);
        cb = bundle.getParcelable(EXTRA_POSITION);
    }

    @Override
    protected void onViewInitialized() {
        claimsRequest = new ClaimsRequest(getHandler());
        WebSettings webSettings = webView.getSettings();
        webSettings.setSupportZoom(true);
        claimsRequest.getPhyDetailsUrl(pb.getReserveId(), GET_PHYSICAL_DETAILS_URL);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_PHYSICAL_DETAILS_URL:
                if (response.isSuccessful) {
                    setPageData((PhysicalExaminationAppointmentBean) response.obj);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    /**
     * 加载页面数据
     */
    private void setPageData(PhysicalExaminationAppointmentBean obj) {
        money.setText(obj.getPrice() + obj.getUnit());
        tvTitle.setText(obj.getTitle());
        if (obj.getRemark().equals("") || obj.getRemark() == null){
            tvHint.setVisibility(View.GONE);
        }else{
            tvHint.setText(obj.getRemark());
        }
        webView.loadDataWithBaseURL(null, ParseHtmlCode.parseHtmlCode(obj.getContent()), "text/html", "utf-8", null);
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    /**
     * 页面监听处理
     */
    @OnClick(R.id.img_appointment)
    void OnClicked() {
        final String number = URLConst.CUSTOMER_SERVICE_TELEPHONE_NUMBERS;
        showConfirmDialog(getString(R.string.contact_customer_service), number, null, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + getString(R.string.call_constomer_server)));
                startActivity(intent);
            }
        }).show();
    }
}
